<script setup>
import { ref, onMounted } from 'vue'
import { getCurrentInstance } from 'vue'

// 获取全局属性中的Socket实例
const { proxy } = getCurrentInstance()
const socket = proxy.$socket

// 空调状态数据
const acStatus = ref({
  deviceId: 'ac-1',
  status: {
    power: false,
    temperature: 25,
    mode: 'cool'
  }
})
const errorMessage = ref('')
const successMessage = ref('')

// 发送空调控制命令
const sendACCommand = async () => {
  try {
    // 通过WebSocket发送命令
    socket.emit('controlAC', acStatus.value)

    successMessage.value = 'Air conditioner command sent successfully'
    setTimeout(() => successMessage.value = '', 3000)
  } catch (error) {
    errorMessage.value = error.message
    setTimeout(() => errorMessage.value = '', 3000)
  }
}

// 切换空调电源
const togglePower = () => {
  acStatus.value.status.power = !acStatus.value.status.power
  sendACCommand()
}

// 增加温度
const increaseTemperature = () => {
  if (acStatus.value.status.temperature < 30) {
    acStatus.value.status.temperature++
    sendACCommand()
  }
}

// 减少温度
const decreaseTemperature = () => {
  if (acStatus.value.status.temperature > 16) {
    acStatus.value.status.temperature--
    sendACCommand()
  }
}

// 切换模式
const toggleMode = () => {
  acStatus.value.status.mode = acStatus.value.status.mode === 'cool' ? 'heat' : 'cool'
  sendACCommand()
}

// 生命周期钩子：组件挂载时
onMounted(() => {
  // 监听空调状态更新
  socket.on('acStatusUpdated', (status) => {
    acStatus.value = status
  })

  // 初始获取空调状态
  fetch('http://localhost:3000/api/device-status')
    .then(response => response.json())
    .then(data => {
      const acData = data.find(device => device.deviceType === 'airConditioner')
      if (acData) {
        acStatus.value = acData
      }
    })
    .catch(error => {
      errorMessage.value = 'Failed to fetch initial AC status'
      setTimeout(() => errorMessage.value = '', 3000)
    })
})
</script>

<template>
  <div class="air-conditioner-control">
    <h2>Air Conditioner Control</h2>

    <!-- 错误提示 -->
    <div v-if="errorMessage" class="error-message">{{ errorMessage }}</div>

    <!-- 成功提示 -->
    <div v-if="successMessage" class="success-message">{{ successMessage }}</div>

    <!-- 空调状态显示 -->
    <div class="ac-status">
      <div class="power-status">
        <span>Power: </span>
        <span :class="acStatus.status.power ? 'on' : 'off'">{{ acStatus.status.power ? 'On' : 'Off' }}</span>
      </div>

      <div class="temperature" v-if="acStatus.status.power">
        <span>Temperature: </span>
        <button @click="decreaseTemperature" class="temp-btn">-</button>
        <span class="temp-value">{{ acStatus.status.temperature }}°C</span>
        <button @click="increaseTemperature" class="temp-btn">+</button>
      </div>

      <div class="mode" v-if="acStatus.status.power">
        <span>Mode: </span>
        <span>{{ acStatus.status.mode === 'cool' ? 'Cool' : 'Heat' }}</span>
      </div>
    </div>

    <!-- 控制按钮 -->
    <div class="control-buttons">
      <button @click="togglePower" :class="acStatus.status.power ? 'power-off' : 'power-on'">
        {{ acStatus.status.power ? 'Turn Off' : 'Turn On' }}
      </button>

      <button @click="toggleMode" v-if="acStatus.status.power">
        Switch to {{ acStatus.status.mode === 'cool' ? 'Heat' : 'Cool' }}
      </button>
    </div>
  </div>
</template>

<style scoped>
.air-conditioner-control {
  max-width: 500px;
  margin: 0 auto;
  padding: 20px;
  font-family: Arial, sans-serif;
  background-color: #f5f5f5;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

h2 {
  color: #333;
  text-align: center;
  margin-bottom: 20px;
}

.ac-status {
  background-color: white;
  padding: 20px;
  border-radius: 8px;
  margin-bottom: 20px;
}

.power-status {
  font-size: 18px;
  margin-bottom: 15px;
}

.on {
  color: #2ecc71;
  font-weight: bold;
}

.off {
  color: #e74c3c;
  font-weight: bold;
}

.temperature {
  margin-bottom: 15px;
  display: flex;
  align-items: center;
}

.temp-btn {
  width: 30px;
  height: 30px;
  background-color: #3498db;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
  margin: 0 10px;
}

.temp-value {
  font-size: 20px;
  font-weight: bold;
  width: 40px;
  text-align: center;
}

.mode {
  font-size: 16px;
}

.control-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
}

button {
  padding: 10px 15px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
}

.power-on {
  background-color: #2ecc71;
  color: white;
}

.power-off {
  background-color: #e74c3c;
  color: white;
}

error-message {
  color: #ff4444;
  background-color: #ffeeee;
  padding: 10px;
  border-radius: 4px;
  margin-bottom: 15px;
  text-align: center;
}

success-message {
  color: #00c851;
  background-color: #e6ffed;
  padding: 10px;
  border-radius: 4px;
  margin-bottom: 15px;
  text-align: center;
}
</style>